/**
 * author:Administrator
 * date:2016/3/14
 * desc: 整体 command 流程
 */

'use strict';
let co = require('co');
let queryService = require('../application/queryService.js');

// int [0,max)
function rdm(max) {
  return parseInt(Math.random() * max);
}


/******************************************************
 *                    用户查询 category                *
 ******************************************************/
function* allCategory() {
  let cates = yield queryService.allCategory();
  console.log(cates);
  console.log(' [x] allCategory success');
}

/******************************************************
 *                    用户查询 location                *
 ******************************************************/
function* allLocation() {
  let cates = yield queryService.allLocation();
  console.log(cates);

  let topCates = yield queryService.topLocation();
  console.log(topCates);
  if (Array.isArray(topCates) && topCates.length > 0) {
    let top0 = topCates[0];
    let top0Child = yield  queryService.childLocation(top0.id);
    console.log('top0Child=', top0Child);
  }
  console.log(' [x] allLocation success');
}


/******************************************************
 *                    用户查询商品                      *
 ******************************************************/
function* customerQueryWare() {
  let cateId = rdmCate().id; // 1; //
  let locId = rdmLoc().id; // 1;  //
  let pageIndex = 1;
  let pageSize = 10;
  let result1 = yield queryService.queryWareByCategoryLocation(cateId, locId, pageIndex, pageSize);
  console.log(result1);

  let result2 = yield queryService.queryWareByCategory(cateId, pageIndex, pageSize);
  console.log(result2);
}


co(function*() {
  yield allCategory();
  yield allLocation();
  console.log(' [x] ****** all done ******')
}).catch(function (err) {
  console.log(err);
  throw err;
});
